package app.tvprogram;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class TvProgramListPanel extends JPanel {
    private JPanel jPanel1;
    private JLabel jLabel1;
    private JComboBox jComboBox1;
    private JButton jButton1;
    private JButton jButton2;
    private JButton jButton3;
    private JPanel jPanel2;
    private JScrollPane jScrollPane1;
    private TvProgramTable jTable1;

    public TvProgramListPanel() {
        super();
        initialize();
    }

    private void initialize() {
        BorderLayout jPanel1Layout = new BorderLayout();
        this.setLayout(jPanel1Layout);
        {
            jPanel1 = new JPanel();
            jLabel1 = new JLabel();
            jPanel1.add(jLabel1);
            jLabel1.setText(Constants.label_tvStation);
            Vector jComboBox1ModelData = new Vector();
            jComboBox1ModelData.add(Constants.label_all);
            jComboBox1ModelData.addAll(TvProgramManager.getInstance()
                    .getAvailableTvStationCollection());
            ComboBoxModel jComboBox1Model = new DefaultComboBoxModel(
                    jComboBox1ModelData);
            jComboBox1 = new JComboBox();
            jPanel1.add(jComboBox1);
            jComboBox1.setModel(jComboBox1Model);
            jButton1 = new JButton();
            jPanel1.add(jButton1);
            jButton1.setText(Constants.label_operationQuery);
            jButton1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent evt) {
                    jButton1MouseClicked(evt);
                }
            });
            jButton2 = new JButton();
            jPanel1.add(jButton2);
            jButton2.setText(Constants.label_operationConfig);
            jButton2.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent evt) {
                    jButton2MouseClicked(evt);
                }
            });
            jButton3 = new JButton();
            jPanel1.add(jButton3);
            jButton3.setText(Constants.label_operationRefresh);
            jButton3.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent evt) {
                    jButton3MouseClicked(evt);
                }
            });
            this.add(jPanel1, BorderLayout.NORTH);
        }
        {
            jPanel2 = new JPanel();
            BorderLayout jPanel12Layout = new BorderLayout();
            jPanel2.setLayout(jPanel12Layout);
            jTable1 = new TvProgramTable();
            this.setTableData(null);
            jScrollPane1 = new JScrollPane();
            jScrollPane1.setViewportView(jTable1);
            jPanel2.add(jScrollPane1, BorderLayout.CENTER);
            this.add(jPanel2, BorderLayout.CENTER);
        }
    }

    private void setTableData(Collection tpCol) {
        Map<Integer, String> ftcMap = TvProgramManager.getInstance()
                .getFavoriteTvChannelMap();
        TvChannel[] tableHeaderData = new TvChannel[ftcMap.size()];
        Integer[] ftcIdArray = ftcMap.keySet().toArray(
                new Integer[ftcMap.keySet().size()]);
        Arrays.sort(ftcIdArray);
        for (int i = 0; i < tableHeaderData.length; i++) {
            TvChannel tc = new TvChannel();
            tc.id = ftcIdArray[i];
            tc.name = ftcMap.get(ftcIdArray[i]);
            tableHeaderData[i] = tc;
        }
        System.out.println("tableHeaderData=" + Arrays.asList(tableHeaderData));
        Map<Integer, List<TvProgram>> ftpMap = new HashMap<Integer, List<TvProgram>>();
        if (tpCol == null) {
            tpCol = TvProgramManager.getInstance()
                    .getAvailableTvProgramCollection();
        }
        for (Iterator it = tpCol.iterator(); it.hasNext();) {
            TvProgram tp = (TvProgram) it.next();
            if (ftcMap.get(tp.getTvChannelId()) != null) {
                if (ftpMap.get(tp.getTvChannelId()) == null) {
                    ftpMap.put(tp.getTvChannelId(), new ArrayList());
                }
                ftpMap.get(tp.getTvChannelId()).add(tp);
            }
        }
        System.out.println("ftpMap=" + ftpMap);
        Object[][] tableData = new Object[24][tableHeaderData.length];
        if (ftpMap.size() > 0) {
            for (int i = 0; i < tableData.length; i++) {
                for (int j = 0; j < tableHeaderData.length; j++) {
                    if (ftpMap.get(tableHeaderData[j].id) != null
                            && ftpMap.get(tableHeaderData[j].id).size() > i) {
                        TvProgram tp = ftpMap.get(tableHeaderData[j].id).get(i);
                        if (tp.getStartDate() != null) {
                            tableData[tp.getStartDate().getHours()][j] = tp;
                        }

                    }
                }
            }
        }
        this.jTable1.repopulateWithData(tableHeaderData, tableData);
    }

    private void jButton1MouseClicked(MouseEvent evt) {
        System.out.println("jButton1.mouseClicked, event=" + evt);
        TvProgram txCondition = new TvProgram();
        txCondition.setTvChannelName(this.jComboBox1.getSelectedItem()
                .toString());
        this.setTableData(TvProgramManager.getInstance()
                .getAvailableTvProgramCollection());
    }

    private void jButton2MouseClicked(MouseEvent evt) {
        System.out.println("jButton2.mouseClicked, event=" + evt);
        TvProgramConfigPanel dialog = new TvProgramConfigPanel(null);
        dialog.setSize(500, 500);
        dialog.setVisible(true);
        this.setTableData(null);
    }

    private void jButton3MouseClicked(MouseEvent evt) {
        System.out.println("jButton3.mouseClicked, event=" + evt);
        TvProgramManager.getInstance().initTvProgram(
                TvProgramManager.getInstance().getFavoriteTvChannelMap(),
                false, false);
        this.setTableData(null);
    }
}
